package com.frxuexi.park.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.AdapterView
import android.widget.AdapterView.OnItemSelectedListener
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.http.Const
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.park.ParkIntent
import com.frxuexi.park.ParkViewModel
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ActivityParkAddBinding
import com.frxuexi.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/park/ParkAddActivity")
@AndroidEntryPoint
class ParkAddActivity : BaseActivity<ActivityParkAddBinding, ParkIntent, ParkViewModel>() {
    override val binding: ActivityParkAddBinding by lazy {
        ActivityParkAddBinding.inflate(
            layoutInflater
        )
    }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()
    var status = "空闲"
    var areaId = 1
    var areaName = "A区停车场"
    var type = "标准车位"
    lateinit var adapter: ArrayAdapter<String>
    val areas = mutableListOf<String>("A区停车场", "B区停车场")
    val types = mutableListOf<String>("标准车位", "VIP车位")
    lateinit var typeAdapter:ArrayAdapter<String>
    @JvmField
    @Autowired
    var id: Int = -1

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.ADD -> {
                                ToastUtils.showLong("添加成功")
                                finish()
                            }

                            RequestType.UPDATE->{
                                ToastUtils.showLong("修改成功")
                                finish()
                            }

                            RequestType.DEFAULT -> {
                                val item = it.data as ParkBeanItem
                                binding.parkaddNo.apply {
                                    setText(item.spaceNumber)
                                    isEnabled = false
                                }
                                binding.parkaddRemark.setText(item.remarks)
                                binding.parkaddUser.setText(item.userName)
                                binding.parkaddCar.setText(item.licensePlate)
                                binding.parkaddStatus.apply {
                                    when (item.spaceStatus) {
                                        "空闲" -> check(R.id.parkadd_kx)
                                        "已占用" -> check(R.id.parkadd_zy)
                                        "维护中" -> check(R.id.parkadd_wh)
                                    }
                                }
                                if (!areas.contains(item.areaName)) {
                                    areas.add(0, item.areaName)
                                    adapter = ArrayAdapter(this@ParkAddActivity, android.R.layout.simple_spinner_item, areas)
                                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                    binding.parkaddArea.adapter = adapter
                                }

                                if(!types.contains(item.spaceType)){
                                    types.add(0,item.spaceType)
                                    typeAdapter = ArrayAdapter(this@ParkAddActivity, android.R.layout.simple_spinner_item, types)
                                    typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                    binding.parkaddType.adapter = typeAdapter
                                }

                            }

                            else -> {}
                        }
                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        binding.headBack.setOnClickListener {
            finish()
        }
        binding.headTitle.setText("编辑车位")
        binding.parkaddSave.setOnClickListener {
            val carNo = binding.parkaddCar.text.toString()
            val remark = binding.parkaddRemark.text.toString()
            val no = binding.parkaddNo.text.toString()
            val user = binding.parkaddUser.text.toString()
            var item = ParkBeanItem(
                areaId, areaName, null, carNo, remark, no, status, type,
                SPUtils.getInstance().getInt(Const.UID), user,0
            )
            if(id!=-1){//修改
                item.id=id
                vm.sendIntent(ParkIntent.addPark(item))
            }else {
                vm.sendIntent(ParkIntent.addPark(item))
            }
        }


        if (id != -1) {
            vm.sendIntent(ParkIntent.getParkById(id!!))
        }


        adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddArea.adapter = adapter
        binding.parkaddArea.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                areaId = position + 1
                areaName = areas.get(position)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

        typeAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, types)
        typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.parkaddType.adapter = typeAdapter
        binding.parkaddType.onItemSelectedListener = object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                type = types.get(position)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }
        }

        binding.parkaddRemark.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                val length = s?.length
                binding.parkaddNum.apply {
                    setText("${length}/10")
                    if (length == 10) {
                        setTextColor(getColor(com.frxuexi.lib_base.R.color.redfont))
                    } else {
                        setTextColor(getColor(com.frxuexi.lib_base.R.color.darkgray))
                    }
                }

            }

            override fun afterTextChanged(s: Editable?) {
            }
        })

        binding.parkaddStatus.setOnCheckedChangeListener { group, checkedId ->
            binding.parkGroup.apply {
                when (checkedId) {
                    R.id.parkadd_kx -> {
                        status = "空闲"
                        visibility = View.GONE
                    }

                    R.id.parkadd_zy -> {
                        status = "已占用"
                        visibility = View.VISIBLE
                    }

                    R.id.parkadd_wh -> {
                        status = "维护中"
                        visibility = View.GONE
                    }
                }
            }

        }
    }


}